# Python TCP Server Code Example

import socket

def setup_server():
    host = ''  # Symbolic name meaning all available interfaces
    port = 12345  # Arbitrary non-privileged port

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print('Socket created')

    try:
        s.bind((host, port))
    except socket.error as msg:
        print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
        return
    print('Socket bind complete')

    s.listen(10)
    print('Socket now listening')

    conn, addr = s.accept()
    
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data:
                break
            print(f"Received message: {data.decode()}")
            reply = "Message received"
            conn.sendall(reply.encode())

if __name__ == "__main__":
    setup_server()